<template>
  <el-card class="m-8">

    <section>

      <section slot="header" class="mb-4 flex items-center">
        <el-button class="mr-4" type="primary" @click="handleAdd()">新增</el-button>
      </section>


      <el-table :data="tableList" align="center" border class="mt-4" stripe style="width: 100%">
        <el-table-column label="奖励ID" prop="id"/>
        <el-table-column label="奖励名称" prop="reward.name"/>
        <el-table-column label="奖励类型" prop="reward_type">
          <template slot-scope="{row}">
            {{ row.reward_type === 'prop' ? '道具' : '宠物道具' }}
          </template>
        </el-table-column>
        <el-table-column label="奖励图片" prop="reward.icon">
          <template slot-scope="{row}">
            <el-image v-if="row.reward && row.reward.icon" :src="row.reward.icon" fit="fill" style="width: 200px;" :lazy="true"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="奖励价格" prop="reward.coin">
          <template slot-scope="{row}">
            {{ row.reward && row.reward.coin ? row.reward.coin : '--' }}
          </template>
          
        </el-table-column>
        <el-table-column label="添加时间" prop="created_at"/>

        <el-table-column align="center" fixed="right" label="操作">
          <template slot-scope="{row}">
            <el-button :loading="detailLoading" plain type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" plain @click="handleSDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <cus-pagination
        :limit.sync="listQuery.perPage"
        :page.sync="listQuery.page"
        :total="meta.total"
        @pagination="initData"
      />
    </section>
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="dialogTitle"
      @close="handleCloseDialog('form')"
      width="30%"
    >
      <el-form
        ref="form"
        :model="formData"
        :rules="Rules"
        label-position="right"
        label-width="120px"
      >

        <el-form-item label="奖励类型" prop="reward_type">
          <el-select v-model="formData.reward_type" clearable filterable placeholder="请选择奖励类型" @change="handleChange">
            <el-option
              v-for="item in rewardType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="奖励ID" prop="reward_id">
          <!-- <el-input v-model="formData.reward_id" placeholder="请输入奖励ID" size="normal" clearable></el-input> -->
          <el-select v-if="formData.reward_type === 'pet-prop'" v-model="formData.reward_id" placeholder="请选择" clearable filterable>
            <el-option v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <el-select v-else v-model="formData.reward_id" placeholder="请选择" clearable filterable>
            <el-option v-for="item in options1"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          
          
        </el-form-item>
        
      </el-form>
      <footer slot="footer">
        <el-button type="danger" @click="dialogFormVisible = false">取消</el-button>
        <el-button type="success" @click="handleOnSubmit">保存</el-button>
      </footer>
    </el-dialog>

  </el-card>
</template>

<script>
import {
  gameLotteryLadderPrize,
  gameLotteryLadderPrizeSave,
  gameLotteryLadderPrizeDel,
  propList
} from '@/api/compositeManagement'
// 礼物下拉数据
import { getData } from '@/api/roomGift/giftList'
// 萌宠道具
import { petPropList } from '@/api/petsManagement'

export default {
  data() {
    return {
      dialogEditVisible: false,
      options: [],
      options1: [],
      title: '新增',
      listQuery: {
        page: 1,
        perPage: 10
      },
      formData: {
        id: undefined,
        reward_type: undefined,
        reward_id: undefined,
      },
      Rules: {
        reward_type: [{ required: true, message: '请选择奖励类型', trigger: 'blur' }],
        reward_id: [{ required: true, message: '请选择奖励', trigger: 'blur' }],
      },
      tableList: [],
      meta: {
        page: 1,
        perPage: 10,
        total: 0
      },

      /* 弹出框开关 */
      dialogFormVisible: false,

      detailLoading: false,
      isEdit: false,
      rewardType: [
        {
          value: 'prop',
          label: '道具'
        },
        {
          value: 'pet-prop',
          label: '宠物道具'
        },
      ],
      dialogTitle: '新增'
    }
  },
  created() {
    this.initData()
    this.getTableData()
    this.getPropList()
  },
  methods: {
    async getTableData() {
        
        const {data, code} = await petPropList({
          page: 1,
          perPage: 1000
        })
        if (code === 200) {
            this.options = data.data
        }

    },
    async getPropList() {
        const {data, code} = await propList({
          page: 1,
          perPage: 1000
        })
        if (code === 200) {
            this.options1 = data.data
        }

    },
    
    handleSDelete(row) {
      this.$confirm('此操作不可恢复，是否继续删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        gameLotteryLadderPrizeDel(row.id).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
          }
          this.dialogFormVisible = false
          this.listQuery.page = 1
          this.initData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleOnSubmit() {

      this.$refs['form'].validate(valid => {
        if (valid) {

          for (let key in this.formData) {
            this.formData[key] = !this.formData[key] ? undefined : this.formData[key]
          }
          gameLotteryLadderPrizeSave(this.formData).then(res => {
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.dialogFormVisible = false
              this.initData()
            }
          })

        }
      })
    },
    handleCloseDialog(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
        this.$refs[formName].resetFields()
      }
      this.formData = {
        id: undefined,
        reward_type: undefined,
        reward_id: undefined,
      }
    },

    handleAdd() {
      this.dialogFormVisible = true
      this.isEdit = false
      this.dialogTitle = '新增'
    },
    handleEdit(row) {
      this.isEdit = true
      this.dialogTitle = '编辑'
      this.formData = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
    },

    async initData() {
      try {
        const { data } = await gameLotteryLadderPrize({
          ...this.listQuery
        })
        this.meta.page = data.page
        this.meta.total = data.total
        this.tableList = data.data
      } catch (e) {
        console.error(e)
      }
    },
    handleChange(val) {
      this.formData.reward_id = undefined
    },

  }
}
</script>

<style scoped>
.search {
  padding-left: 70px;
}

.el-input {
  width: 400px;
}

::v-deep .el-upload-list__item {
  transition: none !important;
}

::v-deep.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
</style>
